<?php
	session_start(); // Khoi dong session
	$ngaygio = date('Y-m-d H:i:s');
	$trinhduyet = $_SERVER['HTTP_USER_AGENT'];
	$ip = $_SERVER['REMOTE_ADDR'];
	mysql_query("insert into counter(ngaygio, trinhduyet, ip) values ('$ngaygio','$trinhduyet', '$ip')");
?>
<?php
	function getRealIpAddr() 
	{    
		if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet    
		{      
			 $ip=$_SERVER['HTTP_CLIENT_IP'];    
		}     
		elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy   
		{    
			 $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];  
		}   
		else   
		{      
			 $ip=$_SERVER['REMOTE_ADDR'];    
		}   
    return $ip; 
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
	<div class="header_phai">Lượt người truy cập</div>
	<div class="box">
		<table border="0" cellpadding="0" cellspacing="4" align="center" width="220">
			<tr>
				<td width="32" align="left"><img src="images/accessonline.gif" width="32" height="14" border="0" /></td>
				<td>
					<?php 
						$sql="SELECT * FROM counter";
						$resultcheck1 = mysql_query($sql);
						
						$rowscheck = mysql_fetch_array($resultcheck1);
						$counter = $rowscheck['counter'];
						
						// nếu ko có người truy cập thì set counter = 1
						if(empty($counter)){
						$counter=1;
						$sql1="INSERT INTO counter(counter) VALUES('$counter')";
						$result1=mysql_query($sql1);
						}
						
						echo "Bạn là vị khách thứ :  ";
						echo $counter;
						
						// tăng giá trị khi có người truy cập
						$addcounter=$counter+1;
						$sql2="update counter set counter='$addcounter'";
						$result2=mysql_query($sql2);
						
						//mysql_close();  
					?>
				</td>
			</tr>
			<tr>
				<td width="32" align="left"><img src="images/12.gif" width="16" height="14" border="0" /></td>
				<td>
					<?php
						
						$tg=time();
						$tgout=900;
						$tgnew=$tg - $tgout;
						
						$sqlquery="insert into useronline(tgtmp,ip,local) values('$tg','$_SERVER[REMOTE_ADDR]','$_SERVER[PHP_SELF]')";
						$queryonline=mysql_query($sqlquery);
						$sqldeleteonline="delete from useronline where tgtmp < $tgnew";
						$querydeleteonline = mysql_query($sqldeleteonline);
						$sqlview="SELECT DISTINCT ip FROM useronline WHERE local='$_SERVER[PHP_SELF]'";
						$queryview=mysql_query($sqlview);
						$user = mysql_num_rows($queryview);
						echo "Số người đang online : $user";
					?>

				</td>
			</tr>
		</table>
	</div>  
</body>
</html>